package com.boilermaker.lotterysystem.dao.mapper;

import com.boilermaker.lotterysystem.dao.dataobject.ActivityUserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ActivityUserMapper {

    @Insert("<script>" +
            " insert into activity_user (activity_id, user_id, user_name, status)" +
            " values" +
            " <foreach item='item' collection='items' separator=','>" +
            "   (#{item.activityId}, #{item.userId}, #{item.userName}, #{item.status})" +
            " </foreach>" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int batchInsert(@Param("items") List<ActivityUserDO> activityUserDOList);

    @Select("select id, user_id, user_name, status" +
            " from activity_user where activity_id = #{activityId}")
    List<ActivityUserDO> selectByActivityId(@Param("activityId") Long activityId);

    @Select("<script>" +
            " select id, user_id, user_name, status" +
            " from activity_user " +
            " where activity_id = #{activityId} and user_id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            "   #{item}" +
            " </foreach>" +
            "</script>")
    List<ActivityUserDO> batchSelect(@Param("activityId") Long activityId,
                                     @Param("items") List<Long> userIds);

    @Update("<script>" +
            " update activity_user set status = #{status}" +
            " where activity_id = #{activityId} and user_id in" +
            " <foreach collection='items' item='item' open='(' separator=',' close=')' >" +
            "   #{item}" +
            " </foreach>" +
            "</script>")
    void batchUpdateStatus(@Param("activityId") Long activityId,
                           @Param("items") List<Long> userIds,
                           @Param("status") String status);
}
